//
//  TimeQuartzView.m
//  AppFinance
//
//  Created by wei on 12-6-10.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "AFTimeQuartzView.h"
#import "AFUtils.h"
#import "AFMarketData.h"

#import "AFSetting.h"

@implementation AFTimeQuartzView

- (void) distory {
    isDistroy = YES;
    
    [self setNeedsDisplay];
}

-(void)drawInContext:(CGContextRef)context rect:(CGRect)rect{

    if (isDistroy) {
        isDistroy = NO;
        [self draw4distory:context];
        return;
    }
    
    // 绘制背景框
    [self drawBackGround:context];
    
    // 开始 绘制 分时 图
    [self drawTimeLine:context];
 
}

- (void) draw4distory:(CGContextRef)context{
    // 绘制前，先清楚所有绘图区域
    CGContextClearRect(context, CGRectMake(0, 0, [AFSetting QUARTZ_TIME_W], [AFSetting QUARTZ_TIME_H]));
    //
    
    // 绘制背景框
    [self drawBackGround:context];
}


// 绘制分时图背景框
-(void) drawBackGround :(CGContextRef)context{
    
    // 四周得矩形框
	CGFloat bg_ling_width = 1.0f;
    CGFloat width = self.frame.size.width;
    CGFloat height = self.frame.size.height;
    // 整个绘图区域大小 320 * 254
    // 绘制前，先清楚所有绘图区域
    CGContextClearRect(context, self.bounds);
    // 设置线的颜色，红色
    CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR);
    // 设置线的宽度
    CGContextSetLineWidth(context, bg_ling_width);
    // [S]整个矩形框范围
    CGContextAddRect(context, CGRectMake(0, 0, width, height)); // 分时背景框
    CGContextStrokePath(context);

    
    // 分时背景虚线
    // 虚线设置
    CGContextSetStrokeColorWithColor(context, AF_K_QUARTZ_DRAW_BORDER_CGCOLOR_daush);
    CGContextSetLineWidth(context, 0.2);
    
   	CGFloat dashPhase = 2.0;
	CGFloat dashPattern[2] = {4.0, 4.0};
	size_t dashCount = 2;
    CGContextSetLineDash(context, dashPhase, dashPattern, dashCount);
    
    // 竖线
    CGContextMoveToPoint(context, width/5, 1);
    CGContextAddLineToPoint(context, width/5, height - 2);
    
    CGContextMoveToPoint(context, width/5*2, 1);
    CGContextAddLineToPoint(context, width/5*2, height - 2);
    
    CGContextMoveToPoint(context, width/5*3, 1);
    CGContextAddLineToPoint(context, width/5*3, height - 2);
    
    CGContextMoveToPoint(context, width/5*4, 1);
    CGContextAddLineToPoint(context, width/5*4, height - 2);
    
    
    // 横线
    CGContextMoveToPoint(context, 1, height/5);
    CGContextAddLineToPoint(context, width - 2, height/5);
    
    CGContextMoveToPoint(context, 1, height/5*2);
    CGContextAddLineToPoint(context, width - 2, height/5*2);
    
    CGContextMoveToPoint(context, 1, height/5*3);
    CGContextAddLineToPoint(context, width - 2, height/5*3);
    CGContextStrokePath(context);
    
    CGContextMoveToPoint(context, 1, height/5*4);
    CGContextAddLineToPoint(context, width - 2, height/5*4);
    CGContextStrokePath(context);
    
    CGContextStrokePath(context);
    CGContextSetLineDash(context, 0, 0, 0); //取消虚线
}


- (void) drawTimeLine:(CGContextRef)context{

    // [S]分时数据
    localTimeListArray = [[AFMarketData data] drawTimeListArray];
    if (localTimeListArray== nil) {
        return;
    }
    
    // 分钟点
    //int s_t_minute_point = [QuartzDefine QD_S_T_MINUTE_POINT];
    // 秒点
    //int s_t_second_point = [QuartzDefine QD_S_T_SECOND_POINT];
    CGFloat bg_ling_width = 1.0;
    CGContextSetLineWidth(context, bg_ling_width);
//    UIColor *mycolor = [AFUtils UIColorFromRGB:0xffffff alpha:1.0];
//    UIColor *mycolor = [AFUtils UIColorFromRGB:0x1878df alpha:1.0];
    UIColor *mycolor = nil;
   // mycolor = [AFUtils UIColorFromRGB:0x356ead alpha:1.0];

     mycolor = K_APP_LIST_FONT_BLACKCOLOR;
    CGContextSetStrokeColorWithColor(context, [mycolor CGColor]);
    
    float x;
    float y;
    NSDictionary *localItem = nil;
    for(int k = 0; k < [localTimeListArray count]; k++){
        localItem = [localTimeListArray objectAtIndex:k];
        
        x = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x"]] floatValue];
        y = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y"]] floatValue];
        
        if(k == 0){
            CGContextMoveToPoint(context, x, y); // 移动到起点坐标
        } else {
            // 增加新的坐标点
            CGContextAddLineToPoint(context, x, y);
        }
    }
    
    CGContextStrokePath(context);
    
    
    
    
    // 填充颜色
//    UIColor *blue = nil;
//    if([[NSUserDefaults standardUserDefaults] boolForKey:DayMode]){
//        blue = [AFUtils UIColorFromRGB:0xedf5fb alpha:0.7];
//    }else{
//        blue = [AFUtils UIColorFromRGB:0x21222d alpha:0.7];
//    }
//    [blue setFill];
//    CGContextSetStrokeColorWithColor(context, [blue CGColor]);
//    for(int k = 0; k < [localTimeListArray count]; k++){
//        localItem = [localTimeListArray objectAtIndex:k];
//        
//        x = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x"]] floatValue];
//        y = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y"]] floatValue];
//        
//        if(k == 0){
//            CGContextMoveToPoint(context, x, y); // 移动到起点坐标
//        } else {
//            // 增加新的坐标点
//            CGContextAddLineToPoint(context, x, y);
//        }
//    }
//    localItem = [localTimeListArray lastObject];
//    x = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x"]] floatValue];
//    CGContextAddLineToPoint(context, x, [AFSetting QUARTZ_TIME_H] - 1);
//    CGContextAddLineToPoint(context, 0,[AFSetting QUARTZ_TIME_H] - 1);
//    CGContextDrawPath(context, kCGPathFillStroke);
    
    // [E]
    
    // [S] 黄色均线
//    UIColor *averagecolor = [AFUtils UIColorFromRGB:0xFA9527 alpha:1.0];
//    CGContextSetStrokeColorWithColor(context, [averagecolor CGColor]);
//    NSArray *timeAverageListArray = [[AFMarketData data] drawTimeAverageListArray];
//    for(int k = 0; k < [timeAverageListArray count]; k++){
//        localItem = [timeAverageListArray objectAtIndex:k];
//        
//        x = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"x"]] floatValue];
//        y = [[NSString stringWithFormat:@"%@",[localItem objectForKey:@"y"]] floatValue];
//        
//        if(k == 0){
//            CGContextMoveToPoint(context, x, y); // 移动到起点坐标
//        } else {
//            // 增加新的坐标点
//            CGContextAddLineToPoint(context, x, y);
//        }
//    }
//    
//    CGContextStrokePath(context);
    // [E]
}

@end
